﻿package org.edu.service.impl;



import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;



import org.edu.bean.Role;
import org.edu.bean.Role_bar;
import org.edu.bean._a_navigationbar;
import org.edu.bean._navigationbar;
import org.edu.dao.NavigationBarDao;
import org.edu.service.NavigationBarService;
import org.springframework.stereotype.Service;

@Service
public class NavigationBarServiceImpl implements
		NavigationBarService {
	
	@Resource
	private NavigationBarDao navigationBarDao ;

	@SuppressWarnings("unchecked")
	public List<_navigationbar> firstBar(List<Role> roles) {
		//List<Role_bar> list = navigationBarDao.selectRole_barFirstFromRole(role);
		
		List<_navigationbar> navigationbars = new ArrayList<_navigationbar>();
		List<Role_bar> roleBars = new ArrayList<Role_bar>() ;
		
		//遍历用户所有的role ，查询出role_bar
		for (Role role : roles) {
			List ls  = navigationBarDao.selectRole_barFromRole(role,"",2);
			// 遍历查询出的List<role_bar>  
			for (int i = 0; i < ls.size(); i++) {
				Role_bar roleBar = (Role_bar)ls.get(i);
				int j ;
				// 遍历roleBars ,如果不重复，就添加进去
				for (j = 0; j < roleBars.size(); j++) {
					if(roleBars.get(j).equals(roleBar)){
						break;
					}
				}
				if(j == roleBars.size()){
					roleBars.add(roleBar);
				}
			}
		}
		for (int i = 0; i < roleBars.size(); i++) {
			_navigationbar navigationbar = navigationBarDao.selectNavigationbars(roleBars.get(i));
			if (navigationbar != null) {
				navigationbars.add(navigationbar);
			}
		}
		return navigationbars;
	}

	@SuppressWarnings("unchecked")
	public List<List> secend_threeBar(List<Role> roles,String n_id ) {
		List<List>  bars = new ArrayList<List>();
		for (Role role : roles) {
			List<Role_bar> roleBars_sec = navigationBarDao.selectRole_barFromRole(role, n_id,6);
			for (Role_bar roleBar : roleBars_sec) {
				_a_navigationbar aNavigationbar = navigationBarDao.select_a_navigationbar(roleBar);
				if (aNavigationbar!=null) {
					List list = new ArrayList();
					List<Role_bar> roleBars_thr = navigationBarDao.selectRole_barFromRole(role, roleBar.getBarid(),11);
					int bars_num  ;
					for(bars_num = 0 ; bars_num<bars.size();bars_num++){
						List ls = bars.get(bars_num);
						if(ls.get(0).equals(aNavigationbar)){
							for (@SuppressWarnings("unused") Role_bar roleBar2 : roleBars_thr) {
								int i ;
								for(i = 1 ; i<ls.size() ; i++){
									if(ls.get(i).equals(roleBars_thr)){
										break;
									}
								}
								if(i == ls.size()){
									ls.add(roleBars_thr);
								}
							}
							break;
						}
					}
					if(bars_num == bars.size()){
						list.add(aNavigationbar) ;
						for (Role_bar roleBar2 : roleBars_thr) {
							list.add(roleBar2);
						}
						bars.add(list);
					}
				}
			}
			for (List ls : bars) {
				for(int i =1 ; i<ls.size() ; i++){
					ls.set(i,navigationBarDao.select_b_navigationbar((Role_bar)ls.get(i)));
				}
			}
		}
		return bars;
	}

	@SuppressWarnings("unchecked")
	public List<List> barLists() {
		List<List> bars = new ArrayList<List>() ;
		List _nList = navigationBarDao.selectAll();
		for(int i= 0 ; i<_nList.size();i++){
			List list = new ArrayList();
			list.add(_nList.get(i));
			List list2 = navigationBarDao.selectFromPid_a_navigationbar((_navigationbar)_nList.get(i));
			for(int j=0 ; j<list2.size();j++){
				List list3 = new ArrayList();
				list3.add(list2.get(j));
				list3.addAll(navigationBarDao.selectFromPid_b_navigationbar((_a_navigationbar)list2.get(j)));
				list.add(list3);
			}
			bars.add(list);
		}
		return bars ;
	}

	public Integer createRoleBar(Role_bar roleBar) {
		return (Integer)navigationBarDao.createBar(roleBar);
	}

	public void delBars(Role role) {
		navigationBarDao.delBars(role);
	}

	public boolean selectRoleBarByRoleIdBarVal(String barid, Integer roleid) {
		List<Role_bar> roleBars = navigationBarDao.selectRoleBarByRoleIdBarVal(barid, roleid);
		if(roleBars.size()>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	public boolean delRoleBar(Role_bar roleBar) {
		return navigationBarDao.delRoleBar(roleBar);
	}

	public Role_bar selectRoleBarByroleidbarid(String barid, Integer roleid) {
		List<Role_bar> roleBars = navigationBarDao.selectRoleBarByRoleIdBarVal(barid, roleid);
		if(roleBars.size()>0){
			return roleBars.get(0);
		}else{
			return null ;
		}
	}

	

}
